package com.woniuxy.base.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.base.model.entity.BaseArea;
import com.woniuxy.base.model.entity.BaseAreaRecord;
import com.woniuxy.base.model.entity.BaseMaterial;
import com.woniuxy.base.service.BaseAreaService;
import com.woniuxy.response.ResponseResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/baseArea")
public class BaseAreaController {
    @Resource
    private BaseAreaService baseAreaService;

    @GetMapping("/all/{current}/{size}")
    public ResponseResult<Page<BaseArea>> all(@PathVariable("current") int current, @PathVariable("size") int size){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",baseAreaService.getAll(current,size));
    }

    @PreAuthorize("hasAuthority('depot:updateIn')")
    @PutMapping("/change")
    public ResponseResult<Boolean> change(@RequestBody BaseArea baseArea){
        String account = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",baseAreaService.update(baseArea,account));
    }

    @PreAuthorize("hasAuthority('depot:addIn')")
    @PostMapping("/add")
    public ResponseResult<Boolean> add(@RequestBody BaseArea baseArea){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",baseAreaService.add(baseArea));
    }

    @PostMapping("/conditionalSearch/{current}/{size}")
    public ResponseResult<Page<BaseArea>> conditionalSearch(@RequestBody BaseArea baseArea, @PathVariable("current") Integer current, @PathVariable("size") Integer size){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",baseAreaService.conditionSearch(baseArea,current,size));
    }

    @GetMapping("/keywordSearch/{current}/{size}")
    public ResponseResult<Page<BaseArea>> keywordSearch(@PathVariable("current") Integer current, @PathVariable("size") Integer size, @RequestParam String keyword){

        return new ResponseResult<>(ResponseResult.SUCCESS,"success",baseAreaService.keywordSearch(keyword,current,size));
    }

    @GetMapping("/detail")
    public ResponseResult<List<BaseAreaRecord>> detail(@RequestParam Integer areaId){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",baseAreaService.detail(areaId));
    }

    @GetMapping("/baseList")
    public ResponseResult<List<BaseArea>> baseList(){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",baseAreaService.baseList());
    }
}
